package edu.pku.hawk.datasource;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Random;

import edu.pku.hawk.controller.util.Config;
import edu.pku.hawk.controller.util.Constants;

public class DataOperation {

	int seed_1, seed_2;

	public boolean CreatTable(ConnectDB con, String sensor_name, boolean isBoolean) throws SQLException,
			ClassNotFoundException {

		String s = "DROP TABLE IF EXISTS `" + sensor_name + "`;";
		con.runSqlUpdate(s);
		if (isBoolean)
			s = "CREATE TABLE `"
					+ sensor_name
					+ "` (`Id` int(11) NOT NULL AUTO_INCREMENT,`value` tinyint(1) DEFAULT NULL,`time` timestamp NULL DEFAULT NULL,`sensor_id` int(11) DEFAULT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
		else
			s = "CREATE TABLE `"
					+ sensor_name
					+ "` (`Id` int(11) NOT NULL AUTO_INCREMENT,`value` float(5,2) DEFAULT NULL,`time` datetime DEFAULT NULL,`sensor_id` int(11) DEFAULT NULL,PRIMARY KEY (`Id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";

		System.out.println(s);
		if (con.runSqlUpdate(s) != 0)
			return true;
		else
			return false;
	}

	public boolean InsertFloatValue(ConnectDB con, int max, String tablename) throws SQLException,
			ClassNotFoundException {

		Random random = new Random(seed_1);
		String s = null;
		SensorData[] tmp = new SensorData[max];
		s = "delete " + tablename + ".* from " + tablename + ";";
		System.out.println(s);

		con.runSqlUpdate(s);
		int i;
		for (i = 1; i <= max; i++) {

			float value = (float) random.nextDouble();
			Timestamp time = new Timestamp(System.currentTimeMillis() - random.nextInt());
			int sensor_id = random.nextInt(seed_2);
			if (sensor_id == 0)
				sensor_id = 1;
			tmp[i - 1] = new SensorData(sensor_id, value, time);

			s = "insert into " + tablename + " (Id,value,time,sensor_id) values (" + i + "," + value + "," + "'" + time
					+ "'" + "," + sensor_id + ")";
			con.runSqlUpdate(s);

		}

		// con.close();
		if (i > 1)
			return true;
		else
			return false;
	}

	public boolean InsertBooleanValue(ConnectDB con, int max, String tablename) throws SQLException,
			ClassNotFoundException {
		int value = 0;
		Random random = new Random(seed_1);
		String s = null;
		SensorData[] tmp = new SensorData[max];
		s = "delete " + tablename + ".* from " + tablename + ";";
		System.out.println(s);

		con.runSqlUpdate(s);
		int i;
		for (i = 1; i <= max; i++) {

			if (random.nextBoolean())
				value = 1;
			else
				value = 0;
			Timestamp time = new Timestamp(System.currentTimeMillis() - random.nextInt());
			int sensor_id = random.nextInt(seed_2);
			if (sensor_id == 0)
				sensor_id = 1;
			tmp[i - 1] = new SensorData(sensor_id, value, time);

			s = "insert into " + tablename + " (Id,value,time,sensor_id) values (" + i + "," + value + "," + "'" + time
					+ "'" + "," + sensor_id + ")";
			con.runSqlUpdate(s);

		}

		// con.close();
		if (i > 1)
			return true;
		else
			return false;
	}

	public boolean InsertFloatValue1(ConnectDB con, String tableShortName) throws SQLException, ClassNotFoundException {

		// int sensor_id = random.nextInt(20);

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		if (idlist == null) {
			int i = 0;
			i++;
		}
		for (String idstr : idlist) {

			Random random = new Random();
			String s = null;
			float value = (float) random.nextDouble();
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "insert into " + Config.Singleton.Instance.getDetectorTableName(tableShortName)
					+ " (value,time,sensor_id) values (" + value + "," + "'" + time + "'" + ","
					+ Integer.valueOf(idstr) + ")";
			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean UpdateFloatValue1(ConnectDB con, String tableShortName) throws SQLException, ClassNotFoundException {

		// int sensor_id = random.nextInt(20);

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		if (idlist == null) {
			int i = 0;
			i++;
		}
		for (String idstr : idlist) {

			Random random = new Random();
			String s = null;
			float value = (float) random.nextDouble();
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "update " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + " set value=" + value
					+ ",time=now()  where sensor_id=" + +Integer.valueOf(idstr);
			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean InsertIntegerValue1(ConnectDB con, String tableShortName) throws SQLException,
			ClassNotFoundException {

		// int sensor_id = random.nextInt(20);

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		if (idlist == null) {
			int i = 0;
			i++;
		}
		for (String idstr : idlist) {

			Random random = new Random();
			String s = null;
			float value = (float) random.nextDouble();
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "insert into " + Config.Singleton.Instance.getDetectorTableName(tableShortName)
					+ " (value,time,sensor_id) values (" + value + "," + "'" + time + "'" + ","
					+ Integer.valueOf(idstr) + ")";
			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean UpdateIntegerValue1(ConnectDB con, String tableShortName) throws SQLException,
			ClassNotFoundException {

		// int sensor_id = random.nextInt(20);

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		if (idlist == null) {
			int i = 0;
			i++;
		}
		for (String idstr : idlist) {

			Random random = new Random();
			String s = null;
			float value = (float) random.nextDouble();
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "update " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + " set value=" + value
					+ ",time=now()   where sensor_id=" + +Integer.valueOf(idstr);

			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean InsertEnumValue1(ConnectDB con, String tableShortName, int max) throws SQLException,
			ClassNotFoundException {

		// int sensor_id = random.nextInt(20);

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		if (idlist == null) {
			int i = 0;
			i++;
		}
		for (String idstr : idlist) {

			Random random = new Random();
			String s = null;
			int value = (int) random.nextInt(max);
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "insert into " + Config.Singleton.Instance.getDetectorTableName(tableShortName)
					+ " (value,time,sensor_id) values (" + value + "," + "'" + time + "'" + ","
					+ Integer.valueOf(idstr) + ")";
			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean InsertBooleanValue1(ConnectDB con, String tableShortName) throws SQLException,
			ClassNotFoundException {
		Random random = new Random();

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		for (String idstr : idlist) {
			int value = 0;
			String s = null;

			if (random.nextBoolean())
				value = 1;
			else
				value = 0;
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "insert into " + Config.Singleton.Instance.getDetectorTableName(tableShortName)
					+ " (value,time,sensor_id) values (" + value + "," + "'" + time + "'" + ","
					+ Integer.valueOf(idstr) + ")";
			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean UpdateBooleanValue1(ConnectDB con, String tableShortName) throws SQLException,
			ClassNotFoundException {
		Random random = new Random();

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		for (String idstr : idlist) {
			int value = 0;
			String s = null;

			if (random.nextBoolean())
				value = 1;
			else
				value = 0;
			Timestamp time = new Timestamp(System.currentTimeMillis());

			s = "update " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + " set value=" + value
			+ ",time=now()   where sensor_id=" + +Integer.valueOf(idstr);

			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-"
					+ Integer.valueOf(idstr) + "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public boolean InsertRFIDValue1(ConnectDB con, String tableShortName) throws SQLException, ClassNotFoundException {
		Random random = new Random();

		Config c = Config.Singleton.Instance;
		int sensor_type = c.getDetectorType(tableShortName);
		List<String> idlist = c.getSensorIdListByType(sensor_type);
		for (String idstr : idlist) {
			String value;
			String s = null;

			if (random.nextBoolean())
				value = Constants.RFID_NUM_OUR_TANK;
			else
				value = Constants.RFID_NUM_UNKNOWN;
			Timestamp time = new Timestamp(System.currentTimeMillis());

			int fakeid = random.nextInt(2);

			s = "insert into " + Config.Singleton.Instance.getDetectorTableName(tableShortName)
					+ " (value,time,sensor_id) values ('" + value + "' ," + "'" + time + "'" + "," + fakeid + ")";
			con.runSqlUpdate(s);

			System.out.println(" " + Config.Singleton.Instance.getDetectorTableName(tableShortName) + "-" + fakeid
					+ "   " + value + "  " + time.toGMTString());
		}
		return true;
	}

	public DataOperation() {
		seed_1 = 100;
		seed_2 = 20;
	}
}
